<?php
$data = $orm->get('pb_sys', 't.name', 'ASC', NULL, NULL, NULL)->d;
?>
<button onclick="goto_newsys();" class="pure-button pure-button-active">New System</button>
<table class="pure-table pure_table_extra" cellpadding="0" cellspacing="0" style="margin-top: 5px;">
  <thead>
    <tr>
      <th class="ui-widget-header ui-corner-left" style="width: 100px;">System</th>
      <th class="ui-widget-header" style="width: 500px;">URL</th>
      <th class="ui-widget-header ui-corner-right" style="width: 50px;">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>

<script type="text/javascript">
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __dir = '<?php echo $dir; ?>';
  var __file = '<?php echo $file; ?>';
  var __home_url = '<?php echo URL . HOME_DIR; ?>';
  var __install_url = '<?php echo URL . INSTALL_DIR; ?>';
  
  $(function(){
    set_data();
  });
  
  function set_data() {
    $('#data_body').empty();
    for (var idx in __data) {
      var itm = __data[idx];
      $('#data_body').append(
          '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
            '<td>' + itm.name + '&nbsp;</td>' +
            '<td>' + __home_url + itm.php_name + '.php</td>' +
            '<td>' +
              '<img src="src/' + __dir + '/images/edit.png" onclick="$.blockUI();location.href=\'' + __install_url + '?d=' + __dir + '&f=index_system&sys=' + itm.id + '\';"></img>' + 
              '<img src="src/' + __dir + '/images/delete.png" onclick="delete_obj(' + itm.id + ', \'' + itm.php_name + '\')"></img>' + 
            '</td>' + 
          '</tr>');
    }
    if (__data.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=3>None</td></tr>');
    }
  }
  
  function delete_obj(id, php_name) {
    var req = {
      id: id,
      php_name: php_name
    };
    
    Ajax.request(
      'firewall.php?d=' + __dir + '&f=' + __file + '&m=delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        Utils.splice_by_id(rslt.id, __data);
        set_data();
      },
      { confirm: 1 }
    );
  }

  function goto_newsys() {
    $.blockUI();
    location.href = '?d=' + __dir + '&f=index_system';
  }
</script>